from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException
from PIL import Image
from io import BytesIO
from time import sleep
import random
import urllib
import urllib.request
import requests
import re
url_add='https://www.bilibili.com/video/'
space_url='https://space.bilibili.com/390157355/video?tid=0&page='
#space_url='https://space.bilibili.com/229733301/video?tid=0&page='
webpage_pat='<span class="be-pager-total">共 (.*?) 页，</span>'
up_name_pat='<span id="h-name">(.*?)</span>'
browser=webdriver.Chrome()
wait = WebDriverWait(browser, 5, 0.2)
browser.get(space_url)
sleep(2)
space_html = browser.page_source
space_page=re.compile(webpage_pat,re.S).findall(space_html)
up_name_list=re.compile(up_name_pat,re.S).findall(space_html)
test_pat='<ul class="clearfix cube-list">(.*?)</ul>'
BV_pat='<li data-aid="(.*?)" class="small-item.*?<a href'
wbe_page=space_page[0]
up_name=up_name_list[0]
print('页数：',wbe_page)
print('UP：',up_name)

sleep(2)
filename='./URL/'+up_name+'.txt'
ft=open(filename,'w+',encoding='utf-8')
for page in range(0,int(wbe_page)):
    url=space_url+str(page+1)+'&keyword=&order=pubdate'
    wait = WebDriverWait(browser, 5, 0.2)
    browser.get(url)
    print(url)
    sleep(2)
    html = browser.page_source
    test_html=re.compile(test_pat,re.S).findall(html)
    print(len(test_html))
    BV=re.compile(BV_pat,re.S).findall(test_html[0])
    for i in range(0,len(BV)):
        print('URL:',url_add+BV[i])
        sleep(0.01)
        ft.write(url_add+BV[i]+'\n')
ft.close()